const router = require("koa-router")();
const { list, bannerList, tabData } = require("../mock/index");

// 列表接口
router.get("/list", (ctx) => {
  // pageSize: 每页数据个数 10， 20， 50， 100
  // pageCode：当前第几个 1，2，3，4，5
  const { pageSize = 10, pageCode = 1, wd = "" } = ctx.request.query;
  let newList = [...list];
  if (wd) {
    newList = list.filter((v) => v.product.name.includes(wd));
  }
        //  2         10                     2
  if ((pageCode -1)  * pageSize > newList.length) {
    ctx.body = {
      code: 400,
      message: "没有更多数据了！",
    };
  } else {
    ctx.body = {
      code: 200,
      data: newList.slice((pageCode - 1) * pageSize, pageCode * pageSize),
    };
  }
});

// 首页轮播接口
router.get("/banner", (ctx) => {
  ctx.body = {
    code: 200,
    data: bannerList,
  };
});

router.get('/tab/data', ctx => {
  ctx.body = {
    code: 200,
    data: tabData 
  }
})

module.exports = router;
